<template>
	<el-button type="primary" @click="add()">
		添加
	</el-button>
	<el-table :data="goodsData">
		<el-table-column prop="gName" label="商品名称">
			
		</el-table-column>
		<el-table-column prop="cName" label="分类名称">
			
		</el-table-column>
		<el-table-column label="操作">
			<template #default="scope">
				
				<el-button type="warning" @click="update(scope.row)">
					修改
				</el-button>
				<el-button color="red" @click="del(scope.row.id)">
					删除
				</el-button>
			</template>
		</el-table-column>
		
	</el-table>
	<el-dialog
	    v-model="dialogVisible"
	    title="添加商品"
	    width="30%"
	  >
	  <el-input placeholder="请输入商品名" v-model="name"></el-input>
	   <el-tree
		  ref="treeRef"
	      :data="categoryData"
	      show-checkbox
	      node-key="id"
	      :props="defaultProps"
		  @check-change="currentChange"
	    />
	    <template #footer>
	      <span class="dialog-footer">
	        <el-button @click="dialogVisible = false">Cancel</el-button>
	        <el-button type="primary" @click="addSubmit"
	          >Confirm</el-button
	        >
	      </span>
	    </template>
	  </el-dialog>
	  <el-dialog
	      v-model="dialogVisible_del"
	      title="删除分类"
	      width="30%"
	    >
	    你确定要删除id为{{id}}这个分类吗？
	      <template #footer>
	        <span class="dialog-footer">
	          <el-button @click="dialogVisible_del = false">Cancel</el-button>
	          <el-button type="primary" @click="delSubmit"
	            >Confirm</el-button
	          >
	        </span>
	      </template>
	    </el-dialog>
</template>

<script setup>
	import {ref} from 'vue'
	import http from './request.js'
	const goodsData = ref([])
	const categoryData = ref([])
	const dialogVisible = ref(false)
	const dialogVisible_del = ref(false)
	const categoryIdArr = ref([])
	const id = ref('')
	const name = ref('')
	const defaultProps = {
	  children: 'children',
	  label: 'name',
	}
	function query(){
		http.get('/goods/queryGoodsCategory').then(res=>{
			goodsData.value = res.data
		})
	}
	query()
	function addSubmit(){
		http.post('/goods/save',{name:name.value,categoryId:categoryIdArr.value[0]}).then(res=>{
			query()
			dialogVisible.value = false
		})
	}
	function add(){
		dialogVisible.value = true
		http.get('/category/query').then(res=>{
			categoryData.value = res.data
		})
	}
	function del(e){
		dialogVisible_del.value = true
		id.value = e
	}
	function delSubmit(){
		http.get('/goods/del?id='+id.value).then(res=>{
			query()
			dialogVisible_del.value = false
		})
	}
	const treeRef = ref()
	function currentChange(e){
		// console.log(e)
		const a = treeRef.value.getCheckedKeys(true)
		categoryIdArr.value = a
		// console.log(a)
	}
</script>

<style>
</style>